package com.jinyang.jetpackdemo.paging3

import android.content.res.Configuration
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.paging.LoadState
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.LinearLayoutManager
import com.jinyang.jetpackdemo.R
import com.jinyang.jetpackdemo.databinding.ActivityPaging3Binding
import com.jinyang.jetpackdemo.util.LjyLogUtil
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

class Paging3Activity : AppCompatActivity() {
    private val viewModel by lazy {
        ViewModelProvider(this).get(Paging3ViewModel::class.java)
    }
    private val mAdapter: RepoAdapter = RepoAdapter()
    private var num1=0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        LjyLogUtil.d("Activity.onCreate:${savedInstanceState}")
        //5. 在Activity中使用
        val binding: ActivityPaging3Binding =
            DataBindingUtil.setContentView(this, R.layout.activity_paging3)
        binding.lifecycleOwner = this
        //下拉刷新
        binding.refreshlayout.setOnRefreshListener {
            mAdapter.refresh()
        }
        binding.recyclerView.layoutManager = LinearLayoutManager(this)
        //6. 添加header,footer
        binding.recyclerView.adapter = mAdapter.withLoadStateFooter(FooterAdapter {
            mAdapter.retry()
        })
//        binding.recyclerView.adapter = repoAdapter.withLoadStateHeaderAndFooter(
//            header = HeaderAdapter { repoAdapter.retry() },
//            footer = FooterAdapter { repoAdapter.retry() }
//        )
//        lifecycleScope.launch {
//            viewModel.getPagingData().collect {
//                mAdapter.submitData(it)
//            }
//        }
        viewModel.getPagingData2().observe(this, {
            mAdapter.submitData(lifecycle, it)
        })
        //7. 监听加载状态
        mAdapter.addLoadStateListener {
            //比如处理下拉刷新逻辑
            when (it.refresh) {
                is LoadState.NotLoading -> {
                    binding.recyclerView.visibility = View.VISIBLE
                    binding.refreshlayout.isRefreshing = false
                }
                is LoadState.Loading -> {
                    binding.refreshlayout.isRefreshing = true
                    binding.recyclerView.visibility = View.VISIBLE
                }
                is LoadState.Error -> {
                    val state = it.refresh as LoadState.Error
                    binding.refreshlayout.isRefreshing = false
                    Toast.makeText(this, "Load Error: ${state.error.message}", Toast.LENGTH_SHORT)
                        .show()
                }
            }
        }
    }
    override fun onStart() {
        super.onStart()
        LjyLogUtil.d("Activity.onStart")
    }

    override fun onRestart() {
        super.onRestart()
        LjyLogUtil.d("Activity.onRestart")
    }

    override fun onResume() {
        super.onResume()
        LjyLogUtil.d("Activity.onResume")
    }

    override fun onPause() {
        super.onPause()
        LjyLogUtil.d("Activity.onPause")
    }

    override fun onStop() {
        super.onStop()
        LjyLogUtil.d("Activity.onStop")
    }

    override fun onDestroy() {
        super.onDestroy()
        LjyLogUtil.d("Activity.onDestroy")
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        LjyLogUtil.d("Activity.onSaveInstanceState")
        outState.putString("key1","value1")
        outState.putString("key2","value2")
    }

    override fun onRestoreInstanceState(savedInstanceState: Bundle) {
        super.onRestoreInstanceState(savedInstanceState)
        LjyLogUtil.d("Activity.onRestoreInstanceState:${savedInstanceState}")
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        LjyLogUtil.d("Activity.onConfigurationChanged:${newConfig}")
    }

    fun onBtnCLick(view: View) {
        when(view.id){
            R.id.btn_num_add -> {
                LjyLogUtil.d("num1=${++num1}")
                LjyLogUtil.d("num2=${++viewModel.num2}")
                viewModel.addNum()
                LjyLogUtil.d("num3=${viewModel.num3.value}")
            }
        }
    }

}